<template>
  <div>
    <el-tree ref="tree" node-key="id" :data="list" :props="{label:'name'}" show-checkbox default-expand-all check-serictly />

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button>取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { tranListToTreeList } from '@/utils'
import { getRolesDetial, assignPrem } from '@/api/setting'
export default {
  props: {
    roleId: String,
    require: true
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        console.log(this.roleId)
        const perIds = await getRolesDetial(this.roleId)
        console.log(perIds, 'getRolesDetial')
        this.$refs.tree.setCheckedKeys(perIds.data.permIds)
        // 保存数据
        this.list = tranListToTreeList(res.data)
      } catch (err) {
        console.log('getPermissionList', err)
      }
    },
    // 点击确定的操作
    async hSubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPrem({ id: this.roleId, permIds: permIds })
        console.log(res)
        // 子传父
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
